package com.wx_zgwl_feign;

import com.netflix.appinfo.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 刘强
 *
 * @author
 * @create 2018-01-14 18:38
 **/
@RestController
@Configurable
public class InvokerController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping(value = "/router")
    public String  router(){
        List<ServiceInstance> ins=getServiceInstances();
        for (ServiceInstance service:ins){
            EurekaDiscoveryClient.EurekaServiceInstance esi=(EurekaDiscoveryClient.EurekaServiceInstance)service;
            InstanceInfo info=esi.getInstanceInfo();
            System.out.println(info.getAppGroupName()+"----"+info.getInstanceId()+"----"+info.getStatus());

        }
        return "";
    }

    /**
     * 查询可用服务
     * @return
     */
    public List<ServiceInstance> getServiceInstances(){
        List<String> ids=discoveryClient.getServices();
        List<ServiceInstance> result=new ArrayList<ServiceInstance>();
        for (String id:ids){
            List<ServiceInstance> ins=discoveryClient.getInstances(id);
            result.addAll(ins);
        }
        return result;
    }
}
